package com.wto;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;

/**
 * Created by wto on 4/15/2017.
 */

public class UtilTeacherSql implements IUtilSql
{
    private UtilSql utilSql=null;
    public UtilTeacherSql()
	{
    	utilSql=UtilSql.getInstance();
	}
    public void insertTeacher(Teacher teacher)
    {
    	utilSql.getConnection();
    	TeacherBean bean=teacher.getBean();
    	String college_id=bean.getCollege_id();
    	String college_name=bean.getCollge_name();
    	String s_id=bean.getS_id();
    	String s_name=bean.getS_name();
    	String teacher_id=bean.getTeacherID();
    	String teacher_name=bean.getTeacherName();

        String sql="insert into teacher_table (college_id,college_name,s_id,s_name,teacher_id,teacher_name) values(?,?,?,?,?,?)";
        List<Object> params=new ArrayList<Object>();
        params.add(college_id);
        params.add(college_name);
        params.add(s_id);
        params.add(s_name);
        params.add(teacher_id);
        params.add(teacher_name);
        
        utilSql.update(sql,params);
        List<DayOfWeek> weekList=teacher.getWeekList();

        for(int i=0;i<weekList.size();i++)
        {
            params.clear();
            params.add(college_id);
            params.add(s_id);
            params.add(teacher_id);
            
            List<Course> courseList=weekList.get(i).getCourseList();
            String sql_one="insert into week_teacher_table values(?,?,?,?,?,?,?,?,?,?)";
            params.add(weekList.get(i).getWeekName());
            params.add(i+"");
            for(int j=0;j<courseList.size();j++)
            {
                params.add(courseList.get(j).toString());
            }
            utilSql.update(sql_one,params);

        }
        utilSql.close();
    }
    public Teacher selectTeacher(TeacherBean bean)
    {
    	utilSql.getConnection();
        boolean flag=false;
        String sql="select t.college_id,t.college_name,t.s_id,t.s_name,t.teacher_id,t.teacher_name,order_t,week_name,course_one,course_two,course_three,course_four,course_five from teacher_table t ,week_teacher_table w where t.college_id=w.college_id and t.s_id = w.s_id and t.teacher_id = w.teacher_id and t.college_id=? and t.s_id=? and t.teacher_id=? order by order_t;";
        List<Object> params=new ArrayList<Object>();
        params.add(bean.getCollege_id());
        params.add(bean.getS_id());
        params.add(bean.getTeacherID());
        Teacher teacher=new Teacher();
        ResultSet resultSet=utilSql.select(sql,params);
        List<DayOfWeek> weekList=new ArrayList<DayOfWeek>();
        try
		{
            while(resultSet.next())
            {
                List<Course> courses=new ArrayList<Course>();
                DayOfWeek weekday=new DayOfWeek();
                bean.setCollge_name(resultSet.getString("college_name"));
                bean.setS_name(resultSet.getString("s_name"));
                bean.setTeacherName(resultSet.getString("teacher_name"));
                //获取课程数据
                courses.add(new Course(resultSet.getString("course_one")));
                courses.add(new Course(resultSet.getString("course_two")));
                courses.add(new Course(resultSet.getString("course_three")));
                courses.add(new Course(resultSet.getString("course_four")));
                courses.add(new Course(resultSet.getString("course_five")));
                //courses.add(new Course(resultSet.getString("course_six")));
                //设置星期几
                weekday.setWeekName(resultSet.getString("week_name"));
                //添加课程数据
                weekday.getCourseList().clear();
                weekday.getCourseList().addAll(courses);
                //添加星期数据
                weekList.add(weekday);
                flag=true;
            }
           teacher.setBean(bean);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
        finally
        {
        	 utilSql.close();
        }
        if(flag)
        {
            teacher.getWeekList().clear();
            teacher.getWeekList().addAll(weekList);
            return teacher;
        }
        else
            return null;
    }
	@Override
	public void insert(Object obj)
	{
		Teacher teacher=(Teacher)obj;
		insertTeacher(teacher);
	}
	@Override
	public Object select(Object obj)
	{
		Teacher teacher=selectTeacher((TeacherBean)obj);
		return teacher;
	}
}
